package com.project.JunM.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.JunM.entity.SCell;
import com.project.JunM.mapper.SCellMapper;
import com.project.JunM.service.SCellService;
import com.project.common.entity.PageVo;
import com.project.common.utils.Serial;
import com.project.xy.entity.DFile;
import com.project.xy.mapper.DFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SCellServiceImpl implements SCellService {

    @Autowired
    private SCellMapper sCellMapper;

    @Autowired
    private DFileMapper dFileMapper;

    /*
    * 安全库存配置管理-制定安全库存配置单(审核标志要为：S001-1,库存分配标志要为：K001-0)
    * */
    @Override
    public PageVo<SCell> SearchProductIdAll(Map<String,String> map) {

        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);

        List<SCell> sCellList = sCellMapper.SearchProductIdAll();

        PageInfo pageInfo = new PageInfo(sCellList);

        PageVo<SCell> pageVo = new PageVo<>();
        pageVo.setTotal(pageInfo.getTotal());
        pageVo.setRows(sCellList);

        return pageVo;
    }

    @Override
    public PageVo<SCell> queryConfigurationAll(Map<String, String> map) {

        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);

        List<SCell> sCellList = sCellMapper.queryConfigurationAll();

        PageInfo pageInfo = new PageInfo(sCellList);

        PageVo<SCell> pageVo = new PageVo<>();
        pageVo.setTotal(pageInfo.getTotal());
        pageVo.setRows(sCellList);

        return pageVo;
    }

    @Override
    public PageVo<SCell> queryChangeProductIdAll(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);

        List<SCell> sCellList = sCellMapper.queryChangeProductIdAll();

        PageInfo pageInfo = new PageInfo(sCellList);

        PageVo<SCell> pageVo = new PageVo<>();
        pageVo.setTotal(pageInfo.getTotal());
        pageVo.setRows(sCellList);

        return pageVo;
    }

    @Override
    public SCell SearchDFileByProductId(String productId) {
        return sCellMapper.SearchDFileByProductId(productId);
    }

    /*
        根据产品编号 修改 库存分配标志 为 已设计（k00-1）
    */
    @Override
    public int insertSelective(SCell record) throws IOException {
        DFile dFile = new DFile();
        dFile.setPRODUCT_ID(record.getProductId());
        //修改  库存分配标志 为 已设计（K001-1）
        //修改  库存分配标志 为 已设计（K001-1）
        dFile.setDESIGN_CELL_TAG("K001-1");

        Serial serial = new Serial();
        serial.addUser("StoreId","400");
        String storeId = serial.getNumber("StoreId", "400", null, null, null);
        System.out.println(storeId);

        record.setStoreId(storeId);

        dFileMapper.updateByProductIdSelective(dFile);

        //CHECK_TAG 审核标志 T001-0 未通过
        record.setCheckTag("T001-0");

        return sCellMapper.insertSelective(record);
    }

    @Override
    public int updateSCellByProductId(SCell record,Integer flag) {
//        flag为标识符 1为复核， 0为变更
        if(flag==1){
            record.setCheckTag("T001-1");
        }else{
            record.setCheckTag("T001-0");

        }
        return sCellMapper.updateSCellByProductId(record);
    }

    @Override
    public SCell querySCellByProductById(String productId) {
        return sCellMapper.querySCellByProductById(productId);
    }

    @Override
    public SCell querySCellByProduct(String productId) {
        return sCellMapper.querySCellByProduct(productId);
    }
}
